package com.lovo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Service;
import com.lovo.bean.ServiceOrder;
import com.lovo.service.IServiceOrderService;
import com.lovo.service.serviceImpl.ServiceOrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServiceOrderServlet" ,urlPatterns = "/serviceOrder.do")
public class ServiceOrderServlet extends HttpServlet {
    IServiceOrderService sosi = new ServiceOrderServiceImpl ();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet (request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding ("UTF-8");
        String method = request.getParameter("method");
        if(method == null || method.equalsIgnoreCase("")){
        }
        else if (method.equalsIgnoreCase ("serviceOrder_page")){
            serviceOrderPage(request,response);
        }
        else if (method.equalsIgnoreCase ("serviceOrder_update")){
            serviceOrderUpdate(request,response);
        }
        else if (method.equalsIgnoreCase ("serviceOrder_del")){
            serviceOrderDel(request,response);
        }
    }

    //      删除
    private void serviceOrderDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int n = Integer.parseInt (request.getParameter ("serviceOrderId"));
        boolean flag = sosi.delServiceOrder(n);
        response.getWriter().write(String.valueOf(flag));
    }

    //     修改
    private void serviceOrderUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServiceOrder serviceOrder = new ServiceOrder ();

        serviceOrder.setServiceOrderId (Integer.parseInt (request.getParameter ("serviceOrderId")));
        serviceOrder.setServiceOrderState (request.getParameter ("serviceOrderState"));
//        System.out.println ("serviceOrder = " + serviceOrder);
        boolean flag = sosi.updateServiceOrder(serviceOrder);
//        System.out.println ("flag = " + flag);
        response.getWriter().write(String.valueOf(flag));

    }

    //    查询所有并分页显示（包含条件查询）
    private void serviceOrderPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = 1;
        int pageSize = 10;
        String page = request.getParameter("page");
        if(page != null){
            pageNo = Integer.parseInt(page);
        }
        String limit = request.getParameter("limit");
        if(limit != null){
            pageSize = Integer.parseInt(limit);
        }
//        ServiceOrder serviceOrder = new ServiceOrder ();
//        serviceOrder.setServiceName (request.getParameter ("serviceName"));
//        System.out.println (request.getParameter ("serviceName"));
        String serviceOrderQuery = request.getParameter ("serviceOrderQuery");

        PageInfo<ServiceOrder> queryPage = sosi.getPageByQuery(pageNo,pageSize,serviceOrderQuery);
        System.out.println ("queryPage = " + queryPage);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(queryPage);
        response.getWriter().write(json);
    }
}
